class register {
	constructor(oText, oPwd, oPwds, obtn) {
		this.oText = oText;
		this.oPwd = oPwd;
		this.oPwds = oPwds;
		this.obtn = obtn;
	}

	phone() {
		// 手机验证
		oText.onblur = function() {
			let xhr = new XMLHttpRequest();
			xhr.open("post", "./php/register.php", true);
			//post的传参方式必须在open方法后设置请求头:将数据以form表单的方式的传递
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xhr.send("phone=" + oText.value);
			xhr.onreadystatechange = function() {
				if (xhr.status == 200 && xhr.readyState == 4) {
					fun(xhr.responseText);
				}
			}

			function fun(resText) {
				let oSpan = document.querySelector(".info1");
				oSpan.innerHTML = resText;
			}
		}
	}

	password() {
		// 密码验证
		//  /(?!^\d+$)(?!^[A-Za-z]+$)(?!^_+$)^\w{6,16}$/;
		let flagpwd = false;
		oPwd.onblur = function() {
			let regPwd = /((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))(?!^.*[\u4E00-\u9FA5].*$)^\S{6,16}$/;
			let oSpan = document.querySelector(".info2");
			if (regPwd.test(oPwd.value) == true) {
				oSpan.innerHTML = "输入合法";
				flagpwd = true;
			} else {
				oSpan.innerHTML = "输入不合法，请重新输入";
			}
		}

		oPwds.onblur = function() {
			let oSpan = document.querySelector(".info3");
			if (oPwds.value == oPwd.value) {
				oSpan.innerHTML = "输入合法";
				flagpwd = true;
			} else {
				oSpan.innerHTML = "输入不合法，请重新输入";
			}
		}



	}

	login() {

		obtn.onclick = function() {
			// 手机验证
			let xhr = new XMLHttpRequest();
			xhr.open("post", "./php/insert.php", true);
			//post的传参方式必须在open方法后设置请求头:将数据以form表单的方式的传递
			xhr.onreadystatechange = function() {
				if (xhr.status == 200 && xhr.readyState == 4) {
					fun(xhr.responseText);
				}
			}
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			let uname = document.getElementById("input-text1").value;
			let pwd = document.getElementById("input-text2").value;
			let msg = `phone=${uname}&pwd=${pwd}`;
			xhr.send(msg);

			function fun(resText) {
				console.log("11");
				window.location.href = "./html/login.html";
			}
		}

	}

}
let oText = document.querySelector(".input-text1");
let oPwd = document.querySelector(".input-text2");
let oPwds = document.querySelector(".input-text3");
let obtn = document.querySelector(".user-btn");
let r = new register(oText, oPwd, oPwds, obtn);

r.phone();
r.password();
r.login();
